/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.storage.service;

import com.alibaba.fastjson.JSON;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.storage.dao.ActivityGoodsSpecDao;
import com.jeesite.modules.storage.dao.CartDao;
import com.jeesite.modules.storage.dao.MemberDao;
import com.jeesite.modules.storage.entity.ActivityGoodsSpec;
import com.jeesite.modules.storage.entity.Cart;
import com.jeesite.modules.storage.util.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 购物车Service
 */
@Service
@Transactional(readOnly=true)
public class CartService extends CrudService<CartDao, Cart> {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private GoodsSpecService goodsSpecService;

    @Autowired
    private ActivityGoodsSpecDao activityGoodsSpecDao;

    @Autowired
    private MemberDao memberDao;

    /**
     * 查询会员购物车列表
     * @param transMemberId 转发会员Id
     * @param memberId 会员Id
     * @param size 每页数量
     * @param page 页码
     * @return 会员购物车列表
     */
    public List<Map> getListByMemberId(String transMemberId, String memberId, Integer size,Integer page)
    {
        String type = memberDao.findTypeById(memberId);
        if(StringUtils.isBlank(type)){
            return  new ArrayList<>();
        }
        List<Map> brandMaps = dao.findBrandListByMemberId(transMemberId,memberId,size * (page - 1),size);
        for (Map brandMap : brandMaps){
            List<Map> cartMaps = dao.findListByBrandId(transMemberId,memberId, String.valueOf(brandMap.get("brandId")));
            for(Map cartMap : cartMaps){
                cartMap.put("sourcePrice",cartMap.get("price"));
                cartMap.put("salePrice", WebUtil.getSalePrice(type,cartMap));
                cartMap.put("specList",goodsSpecService.getActivityListByGoodsId(type, String.valueOf(cartMap.get("activityId")),String.valueOf(cartMap.get("goodsId"))));
            }
            brandMap.put("cartList",cartMaps);
        }
        return brandMaps;
    }

    /**
     * 加入购物车
     * @param transMemberId 转发会员Id
     * @param memberId 会员Id
     * @param transActivityId 转发Id
     * @param goodsSpecId  商品规格Id
     * @return
     */
    @Transactional(readOnly=false)
    public Integer join(String transMemberId,String memberId, String transActivityId,String goodsSpecId) throws Exception {
        Cart cart = new Cart();
        cart.setTransMemberId(transMemberId);
        cart.setMemberId(memberId);
        cart.setTransActivityId(transActivityId);
        cart.setGoodsSpecId(goodsSpecId);

        ActivityGoodsSpec filterActivityGoodsSpec = new ActivityGoodsSpec(goodsSpecId);
        ActivityGoodsSpec activityGoodsSpec = activityGoodsSpecDao.get(filterActivityGoodsSpec);

        if(activityGoodsSpec == null){
            throw  new Exception("参数有误");
        }

        cart.setActivityId(activityGoodsSpec.getActivityId());
        cart.setBrandId(activityGoodsSpec.getBrandId());
        cart.setCateId(activityGoodsSpec.getCateId());
        cart.setGoodsId(activityGoodsSpec.getActivityGoodsId());
        cart.setNum(1);
        super.save(cart);
        return 200;
    }


    /**
     * 保存
     * @param cartId 购物车Id
     * @param goodsSpecId 商品规格Id
     * @param num 数量
     * @param remark 备注
     * @return
     */
    @Transactional(readOnly=false)
    public Integer save(String cartId, String goodsSpecId,Integer num,String remark)
    {
        Cart cart = new Cart(cartId);
        cart.setGoodsSpecId(goodsSpecId);
        cart.setNum(num);
        cart.setRemark(remark);
        super.save(cart);
        return 200;
    }

    /**
     * 删除
     * @param carts 购物车【json】
     * @return
     */
    @Transactional(readOnly=false)
    public Integer remove(String carts)
    {
        ArrayList<String> array = JSON.parseObject(carts, ArrayList.class);
        for(String cartId : array){
            dao.phyDelete(new Cart(cartId));
        }
        return 200;
    }
}